package top.xiaoshuaila.searescue.controller;

import gnu.io.PortInUseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.xiaoshuaila.searescue.pojo.Buoy;
import top.xiaoshuaila.searescue.pojo.Serial;
import top.xiaoshuaila.searescue.pojo.Tag;
import top.xiaoshuaila.searescue.serialport.manager.SerialPortManager;
import top.xiaoshuaila.searescue.serialport.utils.ByteUtils;
import top.xiaoshuaila.searescue.serialport.utils.ShowUtils;
import top.xiaoshuaila.searescue.service.DataDeal;
import top.xiaoshuaila.searescue.service.MqttService;
import top.xiaoshuaila.searescue.service.SerialPortservice;

import javax.servlet.http.HttpServletRequest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

@Controller
@RequestMapping("/setting")
public class setting {
    @Autowired
    private SerialPortservice serialPortservice;
    @Autowired
    private Serial serial;
    @Autowired
    private Buoy buoy;
    @Autowired
    private Tag tag;
    @Autowired
    private DataDeal dataDeal;
    @Autowired
    private MqttService mqttService;
    @GetMapping("")
    public String index(Model model){
        System.out.println(serial);
        model.addAttribute("ports",serialPortservice.findPorts());
        model.addAttribute("port",serial.getPort());
        model.addAttribute("botelv",serial.getBotelv());
        model.addAttribute("jiaoyanwei",serial.getJiaoyanwei());
        model.addAttribute("tingzhiwei",serial.getTingzhiwei());
        model.addAttribute("type",serial.getType());
        model.addAttribute("isopen",serial.isIsopen());
        return "setting";
    }
    @GetMapping("/save")
    @ResponseBody
    public boolean save(Serial serial){
        return serialPortservice.save(serial);
    }
    @GetMapping("/OpenSerial")
    @ResponseBody
    public String OpenSerial(Serial serial){
        return serialPortservice.OpenSerial(serial);
    }
    @GetMapping("/send")
    @ResponseBody
    public void send(String data){
        serialPortservice.send(data);
    }
    @GetMapping("/jiuyuan")
    @ResponseBody
    public boolean jiuyuan(){
        System.out.println("设为救援");
        mqttService.Taskmsg=3;
        serial.setSystemStatus(2);
        return true;
    }
    @GetMapping("/putinwater")
    @ResponseBody
    public boolean putinwater(){
        System.out.println("救生圈落水");
        buoy.setLuoshui(true);
        buoy.setTIME(0);
        return true;
    }
    @GetMapping("/shoudongsubmit")
    @ResponseBody
    public boolean shoudongsubmit(){
        System.out.println("开启手动");
        buoy.setShoudong(true);
        return true;
    }
    @GetMapping("/getshoudong")
    @ResponseBody
    public boolean getshoudong(){
        System.out.println("获取手动状态");
        if(buoy.isShoudong()) {
            buoy.setShoudong(false);
            return true;
        }
        else return false;
    }
    @GetMapping("/fanhang")
    @ResponseBody
    public boolean fanhang(){
        System.out.println("返航");
        buoy.setRTN(true);
        return true;
    }
    @GetMapping("/stoPing")
    @ResponseBody
    public void stoPing(){
        System.out.println("停止");
        buoy.setSTOP(true);
    }
    @GetMapping("/unstop")
    @ResponseBody
    public void unstop(){
        System.out.println("停止停止");
        buoy.setSTOP(false);
    }
    @GetMapping("/stopstop")
    @ResponseBody
    public void stopstop(){
        System.out.println("紧急停止");
        buoy.setSTOPSTOP(true);
    }
    @GetMapping("/setSafe")
    @ResponseBody
    public void setSafe(){
        System.out.println("复位");
        serialPortservice.closeSerialPort();
        serial.setSystemStatus(0);
        serial.setIsopen(false);
        buoy.setSTOP(false);
        buoy.setShoudong(false);
        buoy.setRTN(false);
        buoy.setSTATUS("ACQ");
        buoy.setTIME(0);
        buoy.setVOLT("0V");
        buoy.setTIME(0);
        buoy.setLongitude(114.404729);
        buoy.setLatitude(30.52621);
        buoy.setMOTOR("WORK");
        buoy.setSTOPSTOP(false);
        buoy.setLuoshui(false);
        tag.setSOG("0m/s");
        tag.setDIS("0m");
        tag.setLongitude(114.404729);
        tag.setLatitude(-1);
    }
}
